package com.example.dzcfinal;

import android.app.Activity;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dzcfinal.SQLite.MySQLite;
import com.example.dzcfinal.SQLite.User;

public class ProfileActivity extends Activity {
    private TextView username_value;
    private TextView password_value;
    private TextView sex_value;
    private TextView age_value;
    private Integer userId;
    private ImageView backBtn;
    private Button enterEdit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        Intent intent = getIntent();
        userId= intent.getIntExtra("userId",-1);
        initHandle();
        backProfile();
        EnterEdit();
    }

    private void initHandle(){
        username_value=findViewById(R.id.username_value);
        password_value=findViewById(R.id.password_value);
        sex_value = findViewById(R.id.sex_value);
        age_value = findViewById(R.id.age_value);
        backBtn = findViewById(R.id.backBtn);
        enterEdit = findViewById(R.id.enterEdit);

        MySQLite sql = new MySQLite(ProfileActivity.this);
        Log.i("userId", userId.toString());
        User user = sql.getUser(userId);
        username_value.setText(user.getUsername());
        password_value.setText(user.getPassword().toString());
        sex_value.setText(user.getSex());
        age_value.setText(user.getAge().toString());
    }

    private void backProfile(){
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ProfileActivity.this,MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("userId",userId);
                bundle.putInt("index",2);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void EnterEdit(){
        enterEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ProfileActivity.this,EditActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("username",username_value.getText().toString());
                bundle.putString("password",password_value.getText().toString());
                bundle.putString("sex",sex_value.getText().toString());
                bundle.putString("age",age_value.getText().toString());
                bundle.putInt("userId",userId);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }


}